import os
import sys
from conan import ConanFile, tools
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.files import replace_in_file, apply_conandata_patches, export_conandata_patches

required_conan_version = ">=1.46.0"


class LiquidfunConan(ConanFile):
    name = "liquidfun"
    license = "Zlib"
    description = "LiquidFun is a 2D rigid-body and fluid simulation C++ library for games based upon Box2D"
    topics = ("physics", "engine", "game development")
    homepage = "http://google.github.io/liquidfun/"
    url = "https://github.com/conan-io/conan-center-index"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False],
               "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True,}

    def config_options(self):
        if self.settings.os == "Windows":
            try:
                del self.options.fPIC
            except Exception:
                pass

    def configure(self):
        if self.options.shared:
            try:
                del self.options.fPIC
            except Exception:
                pass
    
    def export_sources(self):
        export_conandata_patches(self)

    def layout(self):
        cmake_layout(self, src_folder="src")

    def source(self):
        tools.files.get(self,
                        **self.conan_data["sources"][self.version],
                        destination=self.source_folder, 
                        strip_root=False)

    @property
    def _python_exe(self):
        return str(sys.executable).replace("\\", "/")

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["BOX2D_BUILD_EXAMPLES"] = False
        tc.variables["PYTHON_EXECUTABLE"] = self._python_exe
        tc.generate()

    def _patch_sources(self):
        apply_conandata_patches(self)
        replace_in_file(self, os.path.join(self.source_folder, "liquidfun", "Box2D", "CMakeLists.txt"),
            "-Werror", "-Wno-error")
        
    def build(self):
        self._patch_sources()
        cmake = CMake(self)
        cmake.configure(build_script_folder="liquidfun/Box2D")
        cmake.build()

    def package(self):
        tools.files.copy(self, "License.txt", src=os.path.join(self.source_folder, "liquidfun", "Box2D"), dst=os.path.join(self.package_folder,"licenses"))
        tools.files.copy(self, os.path.join("Box2D", "*.h"), src=os.path.join(self.source_folder, "liquidfun", "Box2D"), dst=os.path.join(self.package_folder, "include"))
        tools.files.copy(self, "*.lib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
        tools.files.copy(self, "*.dll", src=self.build_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
        tools.files.copy(self, "*.so*", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
        tools.files.copy(self, "*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
        tools.files.copy(self, "*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["liquidfun"]
        if self.settings.os in ("FreeBSD", "Linux"):
            self.cpp_info.system_libs = ["m"]
